package com.sgb.goods.view.activity.details.order;

import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityPurchaseOrderDetailsBinding;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.module.GoodsModule;
import com.sgb.goods.viewmodel.PurchaseOrderDetailsViewModel;
import com.sgb.goods.widget.pop.LogisticsInfoPop2;
import com.sgb.goods.widget.pop.OrderDynamicPop;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;

/**
 * 采购订单详情
 * <pre>
 * 参数：
 *      orderId：订单号
 * </pre>
 *
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_PURCHASE_ORDER_DETAILS)
public class PurchaseOrderDetailsActivity extends BaseActivity<GoodsActivityPurchaseOrderDetailsBinding, PurchaseOrderDetailsViewModel> {

    private String orderId;
    private OrderDynamicPop orderDynamicPop;
    private LogisticsInfoPop2 logisticsInfoPop2;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_purchase_order_details;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(PurchaseOrderDetailsViewModel.class, BR.viewModel);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        // 刷新订单详情
        mViewModel.getOrderDetail(orderId);
    }

    @Override
    protected void init() {
        orderId = getIntent().getStringExtra("orderId");
        mDataBinding.head.line.setVisibility(View.GONE);
        mDataBinding.head.right.setCompoundDrawablesWithIntrinsicBounds(
                ContextCompat.getDrawable(PurchaseOrderDetailsActivity.this, R.mipmap.goods_ic_3point), null, null, null);

        mDataBinding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp1), ContextCompat.getColor(PurchaseOrderDetailsActivity.this, R.color.goods_color_FFF5F5F5)));

        mViewModel.getOrderDetail(orderId);
    }

    @Override
    public void initObserve() {
        mViewModel.orderDynamicRecordsList.observe(this, list -> {
            if (null == list || list.size() <= 0) {
                MToast.showToast(PurchaseOrderDetailsActivity.this, "没有数据");
                return;
            }
            if (null == orderDynamicPop) {
                orderDynamicPop = new OrderDynamicPop(PurchaseOrderDetailsActivity.this);
                orderDynamicPop.setBgView(mDataBinding.grayLayout);
            }
            // 订单动态弹窗
            orderDynamicPop.showPop(mDataBinding.grayLayout, list);
        });
        mViewModel.titleName.observe(this, titleName -> {
            mDataBinding.head.title.setText(titleName);
        });
        mViewModel.logisticsDetailEntity.observe(this, logisticsDetailEntity -> {
            if (null == logisticsInfoPop2) {
                logisticsInfoPop2 = new LogisticsInfoPop2(PurchaseOrderDetailsActivity.this);
                logisticsInfoPop2.setBgView(mDataBinding.grayLayout);
            }
            logisticsDetailEntity.peopleNameStr = mViewModel.orderDetailsEntity.get().name + "  " +
                    mViewModel.orderDetailsEntity.get().contactPhone;
            logisticsDetailEntity.addressStr = mViewModel.orderDetailsEntity.get().fullAddress;
            logisticsInfoPop2.showPop(mDataBinding.grayLayout, logisticsDetailEntity);
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.head.right.setOnClickListener(v -> mViewModel.getOrderDynamicInfo());
        mDataBinding.flInvoice.setOnClickListener(v -> {
            // 跳转发票信息
            ARouter.getInstance().build(Constants.ARouter.AR_INVOICE_DETAIL)
                    .withString("id", String.valueOf(mViewModel.orderDetailsEntity.get().id))
                    .withInt("orderIdentity", mViewModel.orderDetailsEntity.get().orderType.intValue())
                    .navigation();
        });
        mDataBinding.flConvention.setOnClickListener(v -> {
            // 跳转约定信息
            ARouter.getInstance().build(Constants.ARouter.AR_APPOINT_INFO)
                    .withString("orderId", orderId)
                    .navigation();
        });
        mDataBinding.flBuyersRequirements.setOnClickListener(v -> {
            // 跳转采购商要求
            if (!TextUtils.isEmpty(orderId)) {
                ARouter.getInstance().build(Constants.ARouter.AR_TRADING_CONTRACT)
                        .withString("orderId", orderId)
                        .navigation();
            }
        });
        mDataBinding.flContractInfo.setOnClickListener(v -> {
            // 跳转合同信息
            ARouter.getInstance().build(Constants.ARouter.AR_CONTRACT_LIST)
                    .withString("id", String.valueOf(mViewModel.orderDetailsEntity.get().id))
                    .navigation();
        });
        mDataBinding.flPayInfo.setOnClickListener(v -> {
            // 跳转支付信息
            ARouter.getInstance().build(Constants.ARouter.AR_PAY_INFORMATION)
                    .withString("orderId", String.valueOf(mViewModel.orderDetailsEntity.get().id))
                    .withString("orderType", "1")
                    .withInt("orderIdentity", mViewModel.orderDetailsEntity.get().orderType.intValue())
                    .withInt("orderProcessState", mViewModel.orderDetailsEntity.get().orderProcessState)
                    .withInt("paymentWay", mViewModel.orderDetailsEntity.get().paymentWay)
                    .withInt("paymentMethod", mViewModel.orderDetailsEntity.get().paymentMethod)
                    .navigation();
        });
        mDataBinding.tvOrderNotice.setOnClickListener(v -> {
            if (null == mViewModel.orderNoticeEntity) {
                return;
            }
            if ("101".equals(mViewModel.orderNoticeEntity.type)
                    || "102".equals(mViewModel.orderNoticeEntity.type)
                    || "103".equals(mViewModel.orderNoticeEntity.type)
                    || "104".equals(mViewModel.orderNoticeEntity.type)) {
                // 跳转合同详情
                ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                        .withBoolean("isShowOperationLayout", 1 == mViewModel.orderNoticeEntity.state)
                        .withString("contractId", String.valueOf(mViewModel.orderNoticeEntity.extra.contractId))
                        .navigation();
            } else if ("201".equals(mViewModel.orderNoticeEntity.type)
                    || "202".equals(mViewModel.orderNoticeEntity.type)
                    || "203".equals(mViewModel.orderNoticeEntity.type)) {
                // 跳转支付详情
                ARouter.getInstance().build(Constants.ARouter.AR_PAY_INFORMATION)
                        .withString("orderId", String.valueOf(mViewModel.orderDetailsEntity.get().id))
                        .withString("orderType", "1")
                        .withInt("orderIdentity", mViewModel.orderDetailsEntity.get().orderType.intValue())
                        .withInt("orderProcessState", mViewModel.orderDetailsEntity.get().orderProcessState)
                        .withInt("paymentWay", mViewModel.orderDetailsEntity.get().paymentWay)
                        .withInt("paymentMethod", mViewModel.orderDetailsEntity.get().paymentMethod)
                        .navigation();
            } else if ("301".equals(mViewModel.orderNoticeEntity.type)
                    || "302".equals(mViewModel.orderNoticeEntity.type)) {
                // 跳转物流详情
                mViewModel.getLogisticsInfo();
            }
        });
        mDataBinding.tvDeliveryCostTotal.setOnClickListener(v -> {
            if (1 != mViewModel.orderDetailsEntity.get().orderProcessState) {
                ARouter.getInstance().build(Constants.ARouter.AR_LOGISTICS_AGREEMENT)
                        .withString("orderId", String.valueOf(mViewModel.orderDetailsEntity.get().id))
                        .navigation();
            }
        });
        // 点击跳转商品详情
        mViewModel.adapter.setOnItemClickListener((adapter, view, position) -> {
            if (null != mViewModel.adapter.getItem(position)) {
                GoodsModule.getInstance().call1("jumpShopGoodsDetailActivity",
                        mViewModel.adapter.getItem(position).commodityId + "",
                        mViewModel.adapter.getItem(position).skuId + "");
            }
        });
    }
}
